package com.kongjian.manage.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kongjian.manage.common.R;
import com.kongjian.manage.mapper.UserDeptMapper;
import com.kongjian.manage.model.dto.NewUserDeptDTO;
import com.kongjian.manage.model.dto.UpdateUserDeptDTO;
import com.kongjian.manage.model.entity.UserDeptEntity;
import com.kongjian.manage.model.query.UserDeptQuery;
import com.kongjian.manage.model.vo.UserDeptVO;
import com.kongjian.manage.service.IUserDeptService;
import com.kongjian.manage.common.CustomException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
* @author tangjunfeng
* @Classname UserDeptServiceImpl.java
* @Description 部门员工关系表service
* @Date 2023-10-10
* @Version V1.0
*/
@Service
public class UserDeptServiceImpl extends ServiceImpl<UserDeptMapper, UserDeptEntity> implements IUserDeptService {

    @Override
    public R<Page> queryPageUserDept(UserDeptQuery query) throws Exception {
        //构造分页构造器
        Page pageInfo = new Page(query.getPage(), query.getSize());
        LambdaQueryWrapper<UserDeptEntity> lambda = queryWhere(query);

        //执行查询
        Page result = this.page(pageInfo, lambda);
        return R.success(result);
    }

    @Override
    public UserDeptVO getByIdUserDept(Long id) throws Exception {
        UserDeptVO userDeptVO = new UserDeptVO();
        UserDeptEntity userDeptEntity = baseMapper.selectById(id);
        if(userDeptEntity != null) {
            BeanUtils.copyProperties(userDeptEntity,userDeptVO);
        }
        return userDeptVO;
    }

    @Override
    public void addUserDept(NewUserDeptDTO userDeptDTO) throws Exception {
        UserDeptEntity userDept = new UserDeptEntity();
        BeanUtils.copyProperties(userDeptDTO,userDept);
        baseMapper.insert(userDept);
    }

    @Override
    public void modifyUserDept(Long id, UpdateUserDeptDTO userDeptDTO) throws Exception {
        UserDeptEntity userDept = baseMapper.selectById(id);
        if(userDept != null) {
                if(userDeptDTO.getDeptId() != null) {
                userDept.setDeptId(userDeptDTO.getDeptId());
                }
                if(userDeptDTO.getUserId() != null) {
                userDept.setUserId(userDeptDTO.getUserId());
                }
        baseMapper.updateById(userDept);
        }
    }

    @Override
    public void deleteUserDept(Long id) throws Exception {
        UserDeptEntity userDeptEntity = baseMapper.selectById(id);
        if(userDeptEntity == null) {
            throw new CustomException("id:" + id + "没有查到数据");
        }
        baseMapper.deleteById(id);
    }

    @Override
    public Integer countUserDept(UserDeptQuery query) {
        LambdaQueryWrapper<UserDeptEntity> lambda = queryWhere(query);
        return Math.toIntExact(baseMapper.selectCount(lambda));
    }

    private LambdaQueryWrapper<UserDeptEntity> queryWhere(UserDeptQuery query) {
        LambdaQueryWrapper<UserDeptEntity> lambda = new LambdaQueryWrapper<>();
        lambda.eq(query.getDeptId() != null, UserDeptEntity::getDeptId, query.getDeptId());
        lambda.eq(query.getUserId() != null, UserDeptEntity::getUserId, query.getUserId());
        //添加排序条件
        lambda.orderByDesc(UserDeptEntity::getUpdateTime);
        return  lambda;
    }

}
